import {Injectable} from '@angular/core';
import {HttpUtilService} from "../../../../../services/http/http-util.service";
import {environment} from "../../../../../../environments/environment";
import {BarnProduct} from "../entity/barn-product";
import {Observable} from "rxjs";
import {ResponseEntity} from "../../../../../services/http/response-entity";
import {RoleUser} from "../../../basic-data/role/entity/role-user";

@Injectable({
  providedIn: 'root'
})
export class ProductService {
  private prefix = environment.serverName.pp + "/product/"
  private pp_prefix = environment.serverName.pp + "/product-user/"

  constructor(private http: HttpUtilService) {
  }

  query(param: BarnProduct, current = 0, pageSize = 0, handleError = true): Observable<ResponseEntity<BarnProduct[]>> {
    return this.http.getPage(this.prefix + "query", current, pageSize, handleError, param);
  }

  save(param: BarnProduct, handleError = true): Observable<BarnProduct> {
    return this.http.post(this.prefix + "save", param, handleError)
  }

  getById(pid: string, handleError = true): Observable<BarnProduct> {
    return this.http.get(this.prefix + "getById/" + pid, handleError)
  }

  addUsers(productId: string, userIds: string[], handleError = true) {
    return this.http.post(this.pp_prefix + "addUsers", {productId, userIds}, handleError)
  }

  getUsers(productId: string, handleError = true): Observable<RoleUser[]> {
    return this.http.get(this.pp_prefix + "getUsers/" + productId, handleError)

  }

  deleteUser(productId: string, userId: string, handleError = true) {
    return this.http.post(this.pp_prefix + "deleteUser", {productId, userId}, handleError)
  }

  mine(param: BarnProduct, handleError = true): Observable<BarnProduct[]> {
    return this.http.get(this.prefix + "mine", handleError, param)
  }
}
